export function checkPhone(num){
    if(!(/^1[34578]\d{9}$/.test(num))){ 
        //alert("手机号码有误，请重填");  
        return false; 
    }else{
        return true;
    }
}
export function getClientHeight()
{
  var clientHeight=0;
  if(document.body.clientHeight&&document.documentElement.clientHeight)
  {
  var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
  }
  else
  {
  var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
  }
  return clientHeight;
}

export function compress(file, quality, callback) {
    if (!window.FileReader || !window.Blob) {
        return errorHandler('您的浏览器不支持图片压缩')();
    }

    var reader = new FileReader();
    var mimeType = file.type || 'image/jpeg';

    reader.onload = createImage;
    reader.onerror = errorHandler('图片读取失败！');
    reader.readAsDataURL(file);

    function createImage() {
        var dataURL = this.result;
        var image = new Image();
        image.onload = compressImage;
        image.onerror = errorHandler('图片加载失败');
        image.src = dataURL;
    }

    function compressImage() {
        var canvas = document.createElement('canvas');
        var ctx;
        var dataURI;
        var result;

        canvas.width = this.naturalWidth;
        canvas.height = this.naturalHeight;
        ctx = canvas.getContext('2d');
        ctx.drawImage(this, 0, 0);
        dataURI = canvas.toDataURL(mimeType, quality);
        console.log("sldjf"+dataURI)
        result = dataURIToBlob(dataURI);

        callback(null, result);
    }

    function dataURIToBlob(dataURI) {
        var type = dataURI.match(/data:([^;]+)/)[1];
        var base64 = dataURI.replace(/^[^,]+,/, '');
        var byteString = atob(base64);

        var ia = new Uint8Array(byteString.length);
        for (var i = 0; i < byteString.length; i++) {
            ia[i] = byteString.charCodeAt(i);
        }

        return new Blob([ia], {type: type});
    }

    function errorHandler(message) {
        return function () {
            var error = new Error('Compression Error:', message);
            callback(error, null);
        };
    }
}

export class Tab{
    constructor(id){
        this.oTab = document.getElementById(id);
        this.aBtn = this.oTab.getElementsByTagName('li');
        this.init();
    }
    init(){
        for (let i = 0; i < this.aBtn.length; i++) {
            this.aBtn[i].onclick = function(){
                this.hide();
                this.show(i);
                this.isNow = i;
            }.bind(this);
        }
    }
    hide(){
        for (let i = 0; i < this.aBtn.length; i++) {
            this.aBtn[i].className = '';
        }
    }
    show(index){
        this.aBtn[index].className = 'active';
    }
}

